import axios from "axios";

const server = axios.create({
    baseURL: "http://localhost:9000/"
})


// 请求拦截
server.interceptors.request.use((config)=>{
    config.headers["token"]="11111111111111111111"
    return config
})

server.interceptors.response.use((config)=>{
    console.log("config",config)
    return config
})

export const get=(url,params)=>{
    return new Promise((resolve, reject)=>{
        util(url,params,"get",resolve, reject)
    })
}

export const post=(url,params)=>{
    return new Promise((resolve, reject)=>{
        util(url,params,"post",resolve, reject)
    })
}

export const del=(url,params)=>{
    return new Promise((resolve, reject)=>{
        util(url,params,"delete",resolve, reject)
    })
}

export const put= (url,params)=>{
    return new Promise((resolve, reject)=>{
        util(url,params,"put",resolve, reject)
    })
}

const util = (url,params,method,resolve, reject)=>{
    server({
        url,params,method
    }).then(res=>{
        if(res.headers["count"]){
            res.data.pageInfo = {
                count:res.headers["count"],
                pageCount:res.headers["pagecount"],
            }
        }
        resolve(res.data)
    }).catch(err=>{
        reject(err)
    })
}

export default server